package cn.edu.zafu.bigdata.rwx.stusystem;

import java.io.*;
import java.util.*;

public class stuInfo {
    //存放学生信息文件
    private static final String FILE_NAME = "src/main/java/cn/edu/zafu/bigdata/rwx/stusystem/student.dat";
    //学生信息键值对
    private Map<String, Student> studentMap = new HashMap<>();
    private Scanner sc = new Scanner(System.in);

    public static void main(String[] args) {
        stuInfo system = new stuInfo();
        system.loadStuInfo();
        system.start();
    }


    public void start(){
        int option;

        //循环输入
        do{
            printMenu();
            option = sc.nextInt();
            sc.nextLine();
            switch (option){
                case 1 -> addStudent();
                case 2 -> searchStudent();
                case 3 -> updateStudent();
                case 4 -> deleteStudent();
                case 5 -> System.out.println("已退出程序");
                default -> System.out.println("无效输入，请重新输入");
            }
        }while (option != 5);
        //保存学生信息
        saveStudent();
    }

    private void saveStudent() {
        try (ObjectOutputStream oos = new ObjectOutputStream(new FileOutputStream(FILE_NAME))){
            oos.writeObject(studentMap);
        }
        catch (IOException e){
            e.printStackTrace();
        }
    }

    private void deleteStudent() {
        while(true){
            System.out.print("输入学号进行删除（输入'QUIT'退出）: ");
            String id = sc.nextLine();
            if("quit".equalsIgnoreCase(id)){
                break;
            }
            if (studentMap.containsKey(id)) {
                studentMap.remove(id);
                System.out.println("已删除该学生信息");
            }
            else{
                System.out.println("未找到该学生");
            }
        }
    }

    private  void updateStudent() {
        while (true) {
            System.out.print("输入学号进行更新（输入'QUIT'退出）: ");
            String id = sc.nextLine();
            if ("quit".equalsIgnoreCase(id)) {
                break;
            }
            if (studentMap.containsKey(id)){
                System.out.println("请输入要改的姓名");
                String name = sc.nextLine();
                System.out.println("请输入要改的电话");
                String phone = sc.nextLine();
                Student student = new Student(id, name, phone);
                studentMap.put(id, student);
                System.out.println("学生信息已更新");
                System.out.println(student);
            }
            else {
                System.out.println("未找到该学生");
            }
        }
    }

    private void searchStudent() {
        while(true){
            System.out.print("输入学号进行检索（输入'QUIT'退出）: ");
            String id = sc.nextLine();
            if("quit".equalsIgnoreCase(id)){
                break;
            }
            if (studentMap.containsKey(id)){
                Student student = studentMap.get(id);
//                System.out.println("学号："+ student.id+"  姓名："+student.name+"  电话："+ student.phone);
                System.out.println(student);
            }
            else{
                System.out.println("未找到该学生");
            }
        }

    }

    private void addStudent() {
        System.out.println("请输入学号");
        String id = sc.nextLine();
        //判断学号是否存在
        if(studentMap.containsKey(id)){
            System.out.println("学号已经存在，请重新输入");
            return;
        }
        System.out.println("请输入姓名");
        String name = sc.nextLine();
        System.out.println("请输入电话");
        String phone = sc.nextLine();

        Student student = new Student(id, name, phone);
        studentMap.put(id, student);
        System.out.println("已经添加成功");
    }

    private void printMenu() {
        System.out.println("======= 学生信息管理系统 =======");
        System.out.println("1. 添加学生信息");
        System.out.println("2. 检索学生信息");
        System.out.println("3. 更新学生信息");
        System.out.println("4. 删除学生信息");
        System.out.println("5. 退出程序");
        System.out.print("请输入操作编号 (1-5): ");
    }
    private void loadStuInfo(){
        try(ObjectInputStream osi = new ObjectInputStream(new FileInputStream(FILE_NAME))){
            studentMap = (Map<String, Student>) osi.readObject();
        }
        catch (FileNotFoundException|EOFException e){
            System.out.println("学生信息文件不存在，将创建新文件。");
        } catch (IOException | ClassNotFoundException e){
            e.printStackTrace();
        }
    }

    static class Student implements Serializable {
        private String id;
        private String name;
        private String phone;

        public Student(String id, String name, String phone) {
            this.id = id;
            this.name = name;
            this.phone = phone;
        }

        @Override
        public String toString() {
            return "学号: " + id + ", 姓名: " + name + ", 电话: " + phone;
        }
    }
}
